package com.auxgroup.hr.front.service.process.impl;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.exception.CoreException;
import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCWfCleaDtl;
import com.auxgroup.hr.front.dao.domain.PsCWfCleaHdr;
import com.auxgroup.hr.front.dao.domain.PsCWfPlaveHdr;
import com.auxgroup.hr.front.dao.domain.PsCWfPlaveVw;
import com.auxgroup.hr.front.dao.repository.PsCWfCleaDtlRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfCleaHdrRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfPlaveHdrRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfPlaveVwRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.process.WfCleaHdrService;
import com.auxgroup.hr.front.service.process.WfFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * 销假服务
 */
@Service("wfCleaHdrService")
public class WfCleaHdrServiceImpl implements WfCleaHdrService {
    private static final Logger logger = LoggerFactory.getLogger(WfCleaHdrServiceImpl.class);

    @Autowired
    private PsCWfCleaHdrRepository psCWfCleaHdrRepository;
    @Autowired
    private PsCWfPlaveVwRepository psCWfPlaveVwRepository;
    @Autowired
    private PsCWfCleaDtlRepository psCWfCleaDtlRepository;
    @Autowired
    private WfInitServiceImpl wfInitService;

    @Autowired
    private WfFileService wfFileService;

    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;


    @Override
    public PsCWfCleaHdr getWfInfo(String transNum) {
        return psCWfCleaHdrRepository.findOne(transNum);
    }

    @Override
    public List<PsCWfCleaDtl> getDtls(String cTransNbr) {
        return psCWfCleaDtlRepository.findByCTransactionNbr(cTransNbr);
    }

    @Override
    public boolean saveDtls(String cTransNbr, List<String> transNums) {
        try {
            transNums.stream().map(tn -> {
                PsCWfPlaveVw hdr = psCWfPlaveVwRepository.findOne(tn);
                if (hdr == null) {
                    logger.error("流程不存在：" + tn);
                    throw new CoreException(BaseRetCode.CODE_ERROR_INPUT_NOT_RIGHT);
                }
                PsCWfCleaDtl dtl = new PsCWfCleaDtl();
                dtl.setcTransactionNbr(cTransNbr);
                dtl.setcLeaveDays(hdr.getcPleaveDays());
                dtl.setcLeaveType(hdr.getcLeaveType());
                dtl.setcTransactionNum(hdr.getcTransactionNum());
                dtl.setcWfTitle(hdr.getcWfTitle());
                dtl.setcSeccessFlag(" ");
                dtl.setStartDate(hdr.getStartDate());
                dtl.setEndDate(hdr.getEndDate());
                return dtl;
            }).forEach(dtl -> psCWfCleaDtlRepository.save(dtl));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PsCWfCleaHdr saveWfInfo(PsCWfCleaHdr clea, UserDTO user) {
        PsCWfCleaHdr laveHdr = psCWfCleaHdrRepository.findOne(clea.getcTransactionNbr());
        if (laveHdr == null) {
            laveHdr = wfInitService.init(new PsCWfCleaHdr(), user, clea.getcTransactionNbr());
            //FIXME
            laveHdr.setcOtherExplain(clea.getcApplInfo());
        }
        laveHdr.baseInit(clea);
        laveHdr.setLastupdoprid(user.getOprId()); // 更新者
        laveHdr.setLastupddttm(new Timestamp(System.currentTimeMillis())); // 更新时间
        ObjectFieldUtils.handleNullValue(laveHdr);
        return psCWfCleaHdrRepository.save(laveHdr);
    }

    @Override
    public void delWfInfo(String transNum) {
        wfFileService.delWfFiles(transNum, true);
        psCWfCleaHdrRepository.deleteByTransNum(transNum);
    }

}
 